<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="维修任务ID" prop="taskId">
                        <el-input v-model.number="dataForm.taskId" placeholder="维修任务ID"></el-input>
                </el-form-item>
                <el-form-item label="关联的报修单ID" prop="reportId">
                        <el-input v-model.number="dataForm.reportId" placeholder="关联的报修单ID"></el-input>
                </el-form-item>
                <el-form-item label="维修人员姓名" prop="repairerName">
                        <el-input v-model="dataForm.repairerName" placeholder="维修人员姓名"></el-input>
                </el-form-item>
                <el-form-item label="维修时间" prop="repairTime">
                        <el-date-picker
                                v-model="dataForm.repairTime"
                                type="datetime"
                                placeholder="维修时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="维修成本" prop="repairCost">
                        <el-input v-model="dataForm.repairCost" placeholder="维修成本"></el-input>
                </el-form-item>
                <el-form-item label="维修任务状态" prop="status">
                        <el-input v-model="dataForm.status" placeholder="维修任务状态"></el-input>
                </el-form-item>
                <el-form-item label="记录创建时间" prop="createTime">
                        <el-date-picker
                                v-model="dataForm.createTime"
                                type="datetime"
                                placeholder="记录创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="记录更新时间" prop="updateTime">
                        <el-date-picker
                                v-model="dataForm.updateTime"
                                type="datetime"
                                placeholder="记录更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="维修所需工具" prop="requiredTools">
                        <el-input v-model="dataForm.requiredTools" placeholder="维修所需工具"></el-input>
                </el-form-item>
                <el-form-item label="维修所需备件" prop="requiredParts">
                        <el-input v-model="dataForm.requiredParts" placeholder="维修所需备件"></el-input>
                </el-form-item>
                <el-form-item label="维修步骤" prop="repairSteps">
                        <el-input v-model="dataForm.repairSteps" placeholder="维修步骤"></el-input>
                </el-form-item>
                <el-form-item label="更换的零部件" prop="replacedParts">
                        <el-input v-model="dataForm.replacedParts" placeholder="更换的零部件"></el-input>
                </el-form-item>
                <el-form-item label="维修中发现的问题" prop="issuesFound">
                        <el-input v-model="dataForm.issuesFound" placeholder="维修中发现的问题"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    taskId: null,
                    reportId: null,
                    repairerName: null,
                    repairTime: null,
                    repairCost: null,
                    status: null,
                    createTime: null,
                    updateTime: null,
                    requiredTools: null,
                    requiredParts: null,
                    repairSteps: null,
                    replacedParts: null,
                    issuesFound: null,
                },
                dataRule: {
                    taskId: [
                        { required: true, message: '维修任务ID不能为空', trigger: 'blur' }
                    ],
                    reportId: [
                        { required: true, message: '关联的报修单ID不能为空', trigger: 'blur' }
                    ],
                    repairerName: [
                        { required: true, message: '维修人员姓名不能为空', trigger: 'blur' }
                    ],
                    repairTime: [
                        { required: true, message: '维修时间不能为空', trigger: 'blur' }
                    ],
                    repairCost: [
                        { required: true, message: '维修成本不能为空', trigger: 'blur' }
                    ],
                    status: [
                        { required: true, message: '维修任务状态不能为空', trigger: 'blur' }
                    ],
                    createTime: [
                        { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
                    ],
                    updateTime: [
                        { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
                    ],
                    requiredTools: [
                        { required: true, message: '维修所需工具不能为空', trigger: 'blur' }
                    ],
                    requiredParts: [
                        { required: true, message: '维修所需备件不能为空', trigger: 'blur' }
                    ],
                    repairSteps: [
                        { required: true, message: '维修步骤不能为空', trigger: 'blur' }
                    ],
                    replacedParts: [
                        { required: true, message: '更换的零部件不能为空', trigger: 'blur' }
                    ],
                    issuesFound: [
                        { required: true, message: '维修中发现的问题不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppRepairTask/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.apprepairtask
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppRepairTask/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>